﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using CLEX.AdminUI.View;

namespace CLEX.AdminUI.ViewModel
{
    internal sealed class DatabaseListVM : ViewModelBase
    {
        private readonly Facade facade;
        private readonly ObservableCollection<DatabaseReadVM> databases = new ObservableCollection<DatabaseReadVM>();
        private DatabaseReadVM selectedDatabase;
        private RelayCommand newDatabase;
        private RelayCommand editDatabase;
        private RelayCommand deleteDatabase;

        public IEnumerable<DatabaseReadVM> Databases
        {
            get { return databases; }
        }

        public DatabaseReadVM SelectedDatabase
        {
            get { return selectedDatabase; }
            set
            {
                if (selectedDatabase != value)
                {
                    selectedDatabase = value;
                    FirePropertyChanged("SelectedDatabase");
                }
            }
        }

        public ICommand NewDatabase
        {
            get
            {
                if (newDatabase == null)
                {
                    newDatabase = new RelayCommand(DoNewDatabase);
                }
                return newDatabase;
            }
        }

        public ICommand EditDatabase
        {
            get
            {
                if (editDatabase == null)
                {
                    editDatabase = new RelayCommand(DoEditDatabase, CanEditDatabase);
                }
                return editDatabase;
            }
        }

        public ICommand DeleteDatabase
        {
            get
            {
                if (deleteDatabase == null)
                {
                    deleteDatabase = new RelayCommand(DoDeleteDatabase, CanDeleteDatabase);
                }
                return deleteDatabase;
            }
        }

        public DatabaseListVM(Facade facade)
        {
            this.facade = facade;
            Refresh();
        }

        private void Refresh()
        {
            Dictionary<int, string> users = facade.GetAllUsers().ToDictionary(p => p.UserId, p => p.Username);
            databases.Clear();
            databases.AddRange(facade.GetAllDatabases().Select(p => new DatabaseReadVM(p, users[p.OwnerUserId])));
        }

        private void DoNewDatabase()
        {
            DatabaseCreateView vw = new DatabaseCreateView(new DatabaseCreateVM(facade));
            vw.ShowDialog();
            Refresh();
        }

        private void DoEditDatabase()
        {
            DatabaseEditView vw = new DatabaseEditView(new DatabaseEditVM(facade, selectedDatabase.GetData()));
            vw.ShowDialog();
            Refresh();
        }

        private bool CanEditDatabase()
        {
            return SelectedDatabase != null;
        }

        private void DoDeleteDatabase()
        {
            if (facade.ConfirmDelete("database"))
            {
                facade.DeleteDatabase(SelectedDatabase.GetData());
                Refresh();
            }
        }

        private bool CanDeleteDatabase()
        {
            return SelectedDatabase != null;
        }
    }
}
